package javase.javase01.exercise04;
import java.util.Scanner;

/**
 * @author: Gan
 * @date 2021/4/15 0015 17:28
 * @description: 分别自定义编译时异常和运行时异常，并在方法中抛出异常，
 * main方法中调用该方法，并处理相应的异常
 **/
public class MyException extends Exception {
    public MyException() {
    }

    public MyException(String message) {
        super(message);
    }
}

class MyException1 extends RuntimeException {
    public MyException1() {
    }

    public MyException1(String message) {
        super(message);
    }
}

class TestException {
    public static void main(String[] args) {
        //编译时异常
        System.out.println("请输入你的分数：");
        int score = new Scanner(System.in).nextInt();
        if (score < 0 || score > 100) {
            try {
                checkScore1(score);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (score<60){
            try {
                checkScore2(score);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public static void checkScore1(int score) throws MyException {
        if (score < 0 || score > 100)
            throw new MyException("编译期异常,分数不合理");
        System.out.println("考试成绩符合要求！");
    }

    public static void checkScore2(int score) {
        if (score < 60)
            throw new MyException1("考试成绩不合格。运行期异常");
        System.out.println("考试成绩符合要求！");
    }

}